package com.draw.cli.gui.panel.play;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.net.MalformedURLException;
import java.util.Random;

import javax.swing.JButton;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.xml.ws.WebServiceException;

import com.draw.cli.gui.Window;
import com.draw.cli.webclient.DrawingClient;
import com.draw.core.Drawing;
import com.draw.webinterface.DrawingInterface;

public class WordMakerPanel extends JPanel implements ActionListener {
	private static final long serialVersionUID = -2639710693054168175L;

	private static final int MAX_LETTERS = 12;
	private static final String WRONG_WORD = "Ce n'est pas le bon mot !";
	private static final String GOOD_WORD = "Vous avez trouvé le mot !";

	private JButton choices[];
	private JButton reset;
	private JTextField text;

	private Drawing drawing;
	private String letters;

	public WordMakerPanel() {
		this.setLayout(new BorderLayout());
		JPanel top = new JPanel(new FlowLayout());
		JPanel bottom = new JPanel(new FlowLayout());
		this.drawing = null;
		this.choices = new JButton[MAX_LETTERS];
		for (int i = 0; i < MAX_LETTERS; i++) {
			this.choices[i] = new JButton("");
			this.choices[i].setVisible(false);
			this.choices[i].setEnabled(false);
			this.choices[i].addActionListener(this);
			bottom.add(this.choices[i]);
		}
		this.text = new JTextField(MAX_LETTERS);
		this.text.setEditable(false);
		top.add(this.text);
		this.reset = new JButton("Effacer");
		this.reset.addActionListener(this);
		top.add(this.reset);
		this.add(top, BorderLayout.NORTH);
		this.add(bottom, BorderLayout.SOUTH);
	}

	public void initialize(Drawing drawing) {
		this.drawing = drawing;
		this.text.setText("");
		this.letters = shuffleString(this.drawing.getLetters().toUpperCase());
		for (int i = 0; i < MAX_LETTERS; i++) {
			this.choices[i].setText("" + this.letters.charAt(i));
			this.choices[i].setVisible(true);
			this.choices[i].setEnabled(true);
		}
	}

	private void cleanWord() {
		this.text.setText("");
		for (int i = 0; i < this.choices.length; i++) {
			this.choices[i].setEnabled(true);
		}
	}

	private static String shuffleString(String txt) {
		char letters[] = txt.toCharArray();
		Random rd = new Random();
		for (int i = 0; i < letters.length; i++) {
			int p = rd.nextInt(letters.length - 1) + 1;
			char tmp = letters[p];
			letters[p] = letters[0];
			letters[0] = tmp;
		}
		return new String(letters);
	}

	@Override
	public void actionPerformed(ActionEvent arg0) {
		if (arg0.getSource().equals(this.reset)) {
			this.cleanWord();
		} else {
			for (int i = 0; i < this.choices.length; i++) {
				if (arg0.getSource().equals(this.choices[i])) {
					/***/
					char c = this.letters.charAt(i);
					this.text.setText(this.text.getText() + c);
					this.choices[i].setEnabled(false);
					/***/
				}
			}
			if (this.text.getText().length() >= this.drawing.getWord()
					.getWord().length()) {
				if (this.text.getText()
						.equals(this.drawing.getWord().getWord())) {
					JOptionPane.showMessageDialog(Window.getInstance(),
							GOOD_WORD, "Saisie du mot",
							JOptionPane.INFORMATION_MESSAGE);
					this.drawing.setDone(true);
					this.drawing.setSuccessfull(true);
					DrawingInterface di;
					try {
						di = DrawingClient.getInstance();
						this.drawing.unpopulate();
						di.updateDraw(this.drawing);
					} catch (MalformedURLException | WebServiceException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					Window.getInstance().setHome();
				} else {
					JOptionPane.showMessageDialog(Window.getInstance(),
							WRONG_WORD, "Saisie du mot",
							JOptionPane.ERROR_MESSAGE);
					this.cleanWord();
				}
			}
		}
	}
}
